package com.dom.domweb.modular.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.dom.common.enums.PubStatEnum;
import com.dom.common.utils.BeanUtil;
import com.dom.domweb.dao.entity.TMenuInfo;
import com.dom.domweb.dao.service.ITMenuInfoService;
import com.dom.domweb.model.MenuNode;
import com.dom.domweb.modular.shiro.model.ShiroUser;
import com.xiaoleilu.hutool.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description TODO
 * @Author xllei
 * @Date 2018/12/1 7:38 PM
 * @Version 1.0
 **/
@Service
public class MenuTreeService {
    private Map<Integer, String> allMenuMap = null;

    @Autowired
    private ITMenuInfoService menuInfoService;

    public List<MenuNode> getRootList(){
        Wrapper<TMenuInfo> wrapper = new EntityWrapper<>();
        wrapper.eq("menu_type", 1);
        wrapper.eq("stat", PubStatEnum.STAT_NORMAL.getCode());
        List<TMenuInfo> menuInfos = menuInfoService.selectList(wrapper);
        if (ObjectUtil.isNull(menuInfos) || 0 == menuInfos.size()) {
            return null;
        }

        List<MenuNode> fatherNodes = new ArrayList<>();
        menuInfos.forEach(menuInfo -> {
            MenuNode node = toMenuNode(menuInfo, false);
            fatherNodes.add(node);
        });
        return fatherNodes;
    }

    public List<MenuNode> loadMenuTree(ShiroUser user, Integer roleId) {
        List<Integer> roles = new ArrayList<>();
        if(ObjectUtil.isNull(roleId)){
            roles = user.getRoles();
        }else{
            roles.add(roleId);
        }
        List<TMenuInfo> menuInfos = menuInfoService.getMenuList(user.getMemCode(), roles);
//        Wrapper<TMenuInfo> wrapper = new EntityWrapper<>();
//        wrapper.eq("stat", PubStatEnum.STAT_NORMAL.getCode());
//        List<TMenuInfo> menuInfos = menuInfoService.selectList(wrapper);
        if (ObjectUtil.isNull(menuInfos) || menuInfos.size() == 0) {
            return null;
        }

        allMenuMap = menuInfos.stream().collect(Collectors.toMap(TMenuInfo::getId, entity->{return entity.getMenuName();}));

        Map<Integer, List<TMenuInfo>> menuMap = menuInfos.stream().collect(Collectors.groupingBy(TMenuInfo::getMenuType));

        List<TMenuInfo> fatherMenus = menuMap.get(1);
        List<TMenuInfo> leafMenus = menuMap.get(2);
        Map<Integer, List<TMenuInfo>> nodeMap = leafMenus.stream().collect(Collectors.groupingBy(TMenuInfo::getFatherMenuId));

        List<MenuNode> fatherNodes = new ArrayList<>();
        fatherMenus.forEach(fatherMenu->{
            List<MenuNode> childNodes = new ArrayList<>();

            List<TMenuInfo> childMenus = nodeMap.get(fatherMenu.getId());
            if(ObjectUtil.isNotNull(childMenus) && childMenus.size() != 0){
                childMenus.forEach(childMenu->{
                    MenuNode menuNode = toMenuNode(childMenu, true);

                    childNodes.add(menuNode);
                });
            }

            MenuNode fatherNode = toMenuNode(fatherMenu, childNodes == null || 0 == childNodes.size());
            fatherNode.setChildren(childNodes);
            fatherNodes.add(fatherNode);
        });

        return fatherNodes;
    }

    public MenuNode toMenuNode(TMenuInfo menuInfo, boolean isLeaf){
        MenuNode menuNode = new MenuNode();
        MenuNode.BasicData basicData = new MenuNode.BasicData();
        BeanUtil.copyPropertiesIgnoreNull(menuInfo, basicData);
        basicData.setMenuType(menuInfo.getMenuType());
        basicData.setMenuNo(menuInfo.getMenuNo());
        //赋值父菜单名称
        basicData.setFatherMenuName(allMenuMap.get(menuInfo.getFatherMenuId()));
        //赋值节点信息
        menuNode.setId(menuInfo.getId());
        menuNode.setParentId(ObjectUtil.isNull(menuInfo.getFatherMenuId()) ? 0 : menuInfo.getFatherMenuId());
        menuNode.setTitle(menuInfo.getMenuName());
        menuNode.setBasicData(basicData);
        menuNode.setChildren(new ArrayList<>());
        menuNode.setIsLast(isLeaf);
        menuNode.setLevel(menuInfo.getMenuType());
        menuNode.setMenuUrl(menuInfo.getMenuUrl());

        MenuNode.CheckArr checkArr = new MenuNode.CheckArr();
        checkArr.setIsChecked(menuInfo.getStat());
        checkArr.setType(0);
        List<MenuNode.CheckArr> checkArrs = new ArrayList<>();
        checkArrs.add(checkArr);
        menuNode.setCheckArr(checkArrs);
        return menuNode;
    }

//    public List<MenuNode> recuriseNodes(List<TMenuInfo> menuInfos) {
//        if (ObjectUtil.isNull(menuInfos)) {
//            return null;
//        }
//        Map<Integer, List<TMenuInfo>> menuMap = menuInfos.stream().collect(Collectors.groupingBy(TMenuInfo::getMenuType));
//
//        List<MenuNode> nodes = new ArrayList<>();
//        Iterator<Map.Entry<Integer, List<TMenuInfo>>> iterator = menuMap.entrySet().iterator();
//        List<TMenuInfo> leafs = iterator.next().getValue();
//        leafs.forEach(menuInfo -> {
//                    MenuNode node = new MenuNode();
//                    MenuNode.BasicData basicData = new MenuNode.BasicData();
//                    BeanUtil.copyPropertiesIgnoreNull(menuInfo, basicData);
//                    //设置父菜单名称
//                    basicData.setFatherMenuName(allMenuMap.get(menuInfo.getFatherMenuId()).getMenuName());
//
//                    node.setBasicData(basicData);
//                    //设置node信息
//                    node.setId(menuInfo.getId());
//                    node.setTitle(menuInfo.getMenuName());
//                    node.setParentId(menuInfo.getFatherMenuId());
//
//                    nodes.add(node);
//                }
//        );
//        return nodes;
//    }
}
